import Main from '@/components/main'
import parentView from '@/components/parent-view'
import { getDynamicRouter } from '@/libs/util'

/**
 * iview-admin中meta除了原生参数外可配置的参数:
 * meta: {
 *  title: { String|Number|Function }
 *         显示在侧边栏、面包屑和标签栏的文字
 *         使用'{{ 多语言字段 }}'形式结合多语言使用，例子看多语言的路由配置;
 *         可以传入一个回调函数，参数是当前路由对象，例子看动态路由和带参路由
 *  hideInMenu: (false) 设为true后在左侧菜单不会显示该页面选项
 *  notCache: (false) 设为true后页面不会缓存
 *  access: (null) 可访问该页面的权限数组，当前路由设置的权限会影响子路由
 *  icon: (-) 该页面在左侧菜单、面包屑和标签导航处显示的图标，如果是自定义图标，需要在图标名称前加下划线'_'
 *  beforeCloseName: (-) 设置该字段，则在关闭当前tab页时会去'@/router/before-close.js'里寻找该字段名对应的方法，作为关闭前的钩子函数
 * }
 */

export const initRouter = [
  {
    path: '/login',
    name: 'login',
    meta: {
      title: 'Login - 登录',
      hideInMenu: true
    },
    component: () => import('@/view/login/login.vue')
  },
  {
    path: '/',
    name: '_home',
    redirect: '/home',
    component: Main,
    meta: {
      hideInMenu: true,
      notCache: true
    },
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          hideInMenu: true,
          title: '首页',
          notCache: true,
          icon: 'md-home'
        },
        component: () => import('@/view/single-page/home')
      }
    ]
  },
  {
    path: '/401',
    name: 'error_401',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/401.vue')
  },
  {
    path: '/500',
    name: 'error_500',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/500.vue')
  },
  {
    path: '*',
    name: 'error_404',
    meta: {
      hideInMenu: true
    },
    component: () => import('@/view/error-page/404.vue')
  }
]


export const dynamicRouter = [{
  path: '/doc',
  name: 'doc',
  meta: {
    title: '文档',
    href: 'https://lison16.github.io/iview-admin-doc/#/',
    icon: 'ios-book'
  }
},
  {
    path: '/config',
    name: 'config',
    meta: {
      icon: 'md-menu',
      title: '基础管理'
    },
    component: Main,
    children: [
      {
        path: 'role',
        name: 'role',
        meta: {
          icon: 'md-funnel',
          title: '权限管理'
        },
        component: () => import('@/view/config/role.vue')
      },
      {
        path: 'user',
        name: 'user',
        meta: {
          icon: 'md-funnel',
          title: '学生管理'
        },
        component: () => import('@/view/config/user.vue')
      }
    ]
  },
  {
    path: '/cluster',
    name: 'cluster',
    meta: {
      icon: 'md-menu',
      title: '集群管理'
    },
    component: Main,
    children: [
      {
        path: 'host',
        name: 'host',
        meta: {
          icon: 'md-funnel',
          title: '主机管理'
        },
        component: () => import('@/view/cluster/host.vue')
      },
      {
        path: 'monitor',
        name: 'monitor',
        meta: {
          icon: 'md-funnel',
          title: '集群监控'
        },
        component: () => import('@/view/cluster/monitor.vue')
      }
    ]
  },
  {
    path: '/deploy',
    name: 'deploy',
    meta: {
      icon: 'md-menu',
      title: '部署管理'
    },
    component: Main,
    children: [
      {
        path: 'image',
        name: 'image',
        meta: {
          icon: 'md-funnel',
          title: '镜像管理'
        },
        component: () => import('@/view/deploy/image.vue')
      },
      {
        path: 'task',
        name: 'task',
        meta: {
          icon: 'md-funnel',
          title: '任务管理'
        },
        component: () => import('@/view/deploy/task.vue')
      }
    ]
  }
]

/*
export const appRouter = () => {
  //获取后端数据
  debugger
  getAppRouter().then(res => {
    const routerMap = res.data.data
    dynamicRouter.forEach(item => {
      if('children' in item){
        item.children.forEach(childItem => {
          var childPath = item.path + '/' + childItem.path
          var access = routerMap[childPath]
          if(access != null){
            childItem.meta.access = access
          }
        });
      }else{
        var access = routerMap[item.path]
        if(access != null){
          item.meta.access = access
        }
      }

    });

  })

  return dynamicRouter
}
*/

export const filterRouter = getDynamicRouter(dynamicRouter)

export const routes = [ ...initRouter,...filterRouter]

export default routes
